package com.nk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.nk.domain.AjaxRes;
import com.nk.domain.Employee;
import com.nk.domain.Menu;
import com.nk.domain.PageListRes;
import com.nk.mapper.MenuMapper;
import com.nk.service.MenuService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Iterator;
import java.util.List;

@Service
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public PageListRes getMenuList() {

        PageListRes pageListRes = new PageListRes();
        Page<Object> page = PageHelper.startPage(1, 10);
        List<Menu> menuList = menuMapper.selectAll();
        pageListRes.setTotal(page.getTotal());
        pageListRes.setRows(menuList);
        return pageListRes;
    }

    @Override
    public List<Menu> getMenus() {
        return menuMapper.selectAll();
    }

    @Override
    public AjaxRes saveMenu(Menu menu) {
        AjaxRes ajaxRes = new AjaxRes();
        try {
            menuMapper.insert(menu);
            ajaxRes.setSuccess(true);
            ajaxRes.setMessage("添加成功");
        }catch (Exception e){
            ajaxRes.setSuccess(false);
            ajaxRes.setMessage("添加失败");
            e.printStackTrace();
        }
        return ajaxRes;
    }

    @Override
    public AjaxRes updateMenu(Menu menu) {
        AjaxRes ajaxRes = new AjaxRes();

        // 先排除把自己的子菜单设置为父菜单
        List<Menu>childMenuList = menuMapper.getChildMenus(menu.getId());
        for (Menu childMenu : childMenuList) {
            if (childMenu.getId() == menu.getParent().getId()){
                ajaxRes.setSuccess(false);
                ajaxRes.setMessage("不能把子菜单设置为父菜单");
                return ajaxRes;
            }
        }

        try {
            menuMapper.updateByPrimaryKey(menu);
            ajaxRes.setSuccess(true);
            ajaxRes.setMessage("修改成功");
        }catch (Exception e){
            ajaxRes.setSuccess(false);
            ajaxRes.setMessage("修改失败");
            e.printStackTrace();
        }
        return ajaxRes;
    }

    @Override
    public AjaxRes deleteMenu(Long id) {
        AjaxRes ajaxRes = new AjaxRes();
        try {
            // 先设置子菜单为null
            menuMapper.setNullParentMenu(id);
            menuMapper.deleteByPrimaryKey(id);
            ajaxRes.setSuccess(true);
            ajaxRes.setMessage("删除成功");
        }catch (Exception e){
            ajaxRes.setSuccess(false);
            ajaxRes.setMessage("删除失败");
            e.printStackTrace();
        }
        return ajaxRes;
    }

    @Override
    public List<Menu> getMenuTree() {
        List<Menu> menuList = menuMapper.getMenuTree();

        // 获取当前用户
        Subject subject = SecurityUtils.getSubject();
        Employee employee = (Employee) subject.getPrincipal();
        // 根据用户的权限显示对应得菜单
        if (!employee.getAdmin()){
            checkPromission(menuList);
        }
        return menuList;
    }

    public void checkPromission( List<Menu> menuList){

        // 获取当前用户
        Subject subject = SecurityUtils.getSubject();
        Employee employee = (Employee) subject.getPrincipal();

        // 迭代器
        Iterator<Menu> iterator = menuList.iterator();
        while(iterator.hasNext()){
            Menu menu = iterator.next();
            // 判断菜单是否有权限标识
            if (menu.getPermission() != null){
                // 判断是否有该权限
                if (!subject.isPermitted(menu.getPermission().getPresource())){
                    // 没有就删除
                    iterator.remove();
                    continue;
                }
            }

            // 遍历子菜单
            if (menu.getChildren().size() > 0){
                checkPromission(menu.getChildren());
            }
        }


    }
}
